package com.yhj.reggie.controller;

import com.yhj.reggie.common.R;
import com.yhj.reggie.entity.ShoppingCart;
import com.yhj.reggie.service.ShoppingCarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCarService shoppingCarService;
    @RequestMapping("/add")
    public R<ShoppingCart> addCart(@RequestBody ShoppingCart shoppingCart, HttpSession session){
        Long id = (Long) session.getAttribute("user");
        shoppingCart.setUserId(id);
        ShoppingCart shoppingCartResult = shoppingCarService.addCart(shoppingCart);
        return R.success(shoppingCartResult);
    }

    @RequestMapping("/list")
    public R<List<ShoppingCart>> showShoppingCart(HttpSession session){
        Long id = (Long) session.getAttribute("user");
        List<ShoppingCart> shoppingCarts= shoppingCarService.findAllByUser(id);
        return R.success(shoppingCarts);
    }

    @DeleteMapping("/clean")
    public R<String> deleteCart(HttpSession session){
        Long id = (Long) session.getAttribute("user");
        shoppingCarService.deleteAll(id);
        return R.success("已清空购物车");
    }
}
